package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduMajorDTO;
import com.zenith.edu.po.vo.EduMajorVO;
import com.zenith.edu.service.EduMajorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/major")
@Tag(name = "专业管理", description = "专业管理接口")
public class EduMajorController {
    
    @Autowired
    private EduMajorService eduMajorService;
    
    @SecurePostMapping(value = "/createMajor", roles = "ROLE_ADMIN", permission = "major:create")
    @Operation(summary = "创建专业")
    public Result<EduMajorVO> createMajor(@RequestBody EduMajorDTO dto) {
        EduMajorVO majorVO = eduMajorService.createMajor(dto);
        return Result.success(majorVO);
    }
    
    @SecurePostMapping(value = "/getMajorById/{id}", roles = "ROLE_ADMIN", permission = "major:read")
    @Operation(summary = "根据ID获取专业详情")
    public Result<EduMajorVO> getMajorById(@PathVariable Long id) {
        EduMajorVO majorVO = eduMajorService.getMajorById(id);
        if (majorVO == null) {
            return Result.fail("专业不存在");
        }
        return Result.success(majorVO);
    }
    
    @SecurePostMapping(value = "/updateMajor", roles = "ROLE_ADMIN", permission = "major:update")
    @Operation(summary = "更新专业信息")
    public Result<EduMajorVO> updateMajor(@RequestBody EduMajorDTO dto) {
        EduMajorVO majorVO = eduMajorService.updateMajor(dto);
        return Result.success(majorVO);
    }
    
    @SecurePostMapping(value = "/deleteMajor/{id}", roles = "ROLE_ADMIN", permission = "major:delete")
    @Operation(summary = "删除指定专业")
    public Result<Void> deleteMajor(@PathVariable Long id) {
        boolean result = eduMajorService.deleteMajor(id);
        if (!result) {
            return Result.fail("专业删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getMajorList", roles = "ROLE_ADMIN", permission = "major:read")
    @Operation(summary = "获取专业列表")
    public Result<List<EduMajorVO>> getMajorList() {
        List<EduMajorVO> majorList = eduMajorService.getMajorList();
        return Result.success(majorList);
    }
}